﻿using rmTest.Common.ConstUtil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rmTest.Common
{
    public class LogUtil
    {
        private static string _FilePath;
        private static LogUtil _instance = null;
        public static LogUtil Instance
        {
            get
            {
                _instance = new LogUtil();
                string logPath = ConfigurationArgs.LogPath;
                if (string.IsNullOrEmpty(logPath))
                {
                    logPath = System.AppDomain.CurrentDomain.BaseDirectory;
                }
                logPath += "/log_" + DateTime.Now.ToString("yyyyMM");// + "/" + DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(logPath))
                {
                    Directory.CreateDirectory(logPath);
                }
                _FilePath = logPath + "/" + DateTime.Now.ToString("yyyy-MM") + ".txt";
                return _instance;
            }
        }

        private LogUtil()
        {
        }

        public void PrintLog(string logContent)
        {
            logContent = logContent + "\r\n";
            //输出
            foreach (var item in logContent)
            {
                Console.Write(item);
                System.Threading.Thread.Sleep(50);
            }
            //Console.Write(logContent);

            int len = logContent.Length;
            byte[] inputByts = new byte[len];
            inputByts = Encoding.GetEncoding("utf-8").GetBytes(logContent);
            System.IO.FileStream fs = new System.IO.FileStream(_FilePath, System.IO.FileMode.OpenOrCreate);
            fs.Seek(0, System.IO.SeekOrigin.End);
            fs.Write(inputByts, 0, inputByts.Length);
            fs.Close();//关闭流 
        }
    }
}
